<template>
  <view class="take-list">
    <my-header title="选择需要提货的商品"></my-header>
    <my-content class="content">
      <view class="take_box">
        <view class="goods_box"
              :style="[useLevelBg('item', item.level_id)]"
              @click="selected(item, index)" v-for="(item, index) in orderData" :key="item.id">
          <view class="goods_content">
            <image v-if="item.selected" class="selected" src="https://www.dingxians.cn/images/common/selected.png"></image>
            <image v-else class="selected" src="https://www.dingxians.cn/images/common/unselected.png"></image>
            <view class="goods-image-bg" :style="[useLevelBg('img', item.level_id)]">
              <image mode="widthFix" class="goods_image" :src="item.box_prize_image"></image>
              <view
                class="level-icon"
                :style="[useLevelBg('icon', item.level_id)]"
              >
                {{ item.level_name }}
              </view>
            </view>
            <view class="goods_info">
              <view class="goods_name">{{ item.box_prize_name }}</view>
              <view class="goods_price_box">
                <view class="goods_price">
                  <view>￥</view>
                  <view class="goods_price_int">{{ item.price }}</view>
                  <view class="goods_price_ext">参考价</view>
                </view>
                <view class="goods_num">
                  x {{ item.num }}
                </view>
              </view>
            </view>
          </view>
        </view>
      </view>
      <view style="visibility: hidden;">1</view>
      <view class="freight">
        <view class="freight_title"><!-- 单次提货满3件及以上包邮（偏远地区除外） --></view>
        <view class="freight_rule" @click="header('/packageB/content/freight')">运费规则 ></view>
      </view>
      <view class="bottom">
        <view class="bottom_select" @click="allQuery()">
          <image class="bottom_select_icon" src="https://www.dingxians.cn/images/common/unselected.png" v-if="!allSelected"></image>
          <image class="bottom_select_icon" src="https://www.dingxians.cn/images/common/selected.png" v-if="allSelected"></image>
          <view style="font-size: 24rpx;">全选</view>
          <view class="bottom_text">
            <view>已选</view>
            <view class="bottom_text_num">{{ num }}</view>
            <view class="bottom_txt">件</view>
          </view>
        </view>

        <view class="exchange_btn" v-if="num == 0">立即提货</view>
        <view class="exchange_btn exchange" v-else @click="take()">立即提货</view>
      </view>
    </my-content>
  </view>
</template>

<script>
import api from '../../api/index.js'
import box from '@/mixins/box'

export default {
  mixins: [box],
  data() {
    return {
      orderData: [],
      id: '',
      allSelected: false,
      page: 1,
      lastPage: 1,
      isWelfare: false,
    }
  },
  onLoad(option) {
    this.id = option.id
    this.isWelfare = option.welfare === 'true'
    this.getGoods()
  },
  onShow() {
  },

  onReachBottom() {
    if (this.page > this.lastPage) {
      this.show('已经到底了')
      return
    }
    this.getGoods()
  },

  computed: {
    // 选中的商品
    num() {
      let total_num = 0
      this.orderData.filter(item => {
        return item.selected == true
      }).forEach((item) => {
        total_num += item.num
      })
      return total_num
    },
  },
  methods: {
    splitNum(num) {
      // if (typeof(num) == 'number') {
      return num.toString().split('.')
      // }
      // return num.split('.')

    },
    allQuery() {
      this.allSelected = !this.allSelected

      for (let i = 0; i < this.orderData.length; i++) {
        this.orderData[i].selected = this.allSelected
        this.$set(this.orderData, i, this.orderData[i])
      }
    },
    selected(item, index) {
      item.selected = !item.selected
      this.$set(this.orderData, index, item)
    },
    getGoods() {
      uni.showLoading({
        title: '数据加载中',
      })
      let promise
      if (this.isWelfare) {
        promise = api.welfareHouses.takeShow({ page: this.page })
      } else {
        promise = api.blindBoxOrderPrizes.takeShow({ page: this.page })
      }
      promise.then(res => {
        if (res.code === 200) {
          uni.hideLoading()
          this.orderData = this.orderData.concat(res.data.data)
          this.lastPage = res.data.lastPage
          this.page++
        }
      }).catch(error => {
        console.log(error)
      })
    },
    take() {
      let ids = this.orderData.filter(item => {
        return item.selected == true
      }).map(item => {
        return item.id + ':' + item.num
      })
      if (ids.length > 100) {
        this.show('一次最多提货100件')
        return
      }
      // ids = JSON.stringify(ids)
      console.log(ids)
      this.header(`/pages/box/order?ids=${ids}&welfare=` + this.isWelfare)
    },
  },
}
</script>

<style lang="scss">
.take-list {
  min-height: 100vh;
  height: 100%;
  // background-color: #f7f7f7;
  background: #141414;
}
.content {
  min-height: 100vh;
  height: 100%;
  // background-color: #f7f7f7;
  background: #141414;

  .take_box {
    display: flex;
    flex-direction: column;
    margin: 0 20rpx;
    // border-radius: 5rpx;
    padding-top: 20rpx;
    margin-bottom: 200rpx;

    .goods_box {
      display: flex;
      background-size: 100% 100%;
      border-radius: 10rpx;
      margin-bottom: 20rpx;
      flex-direction: column;
      padding: 30rpx 0;
      color: white;

      .goods_level {
        color: #fff;
        font-size: 24rpx;
        padding: 10rpx 0;
        margin-top: 10rpx;
        border-top-right-radius: 20rpx;
        width: 100rpx;
        border-bottom-right-radius: 20rpx;
        display: flex;
        justify-content: center;
        align-items: center;
      }

      .goods_content {
        align-items: center;
        display: flex;
        height: 200rpx;

        .selected {
          width: 32rpx;
          height: 32rpx;
          margin-left: 40rpx;
        }

        .goods-image-bg {
          position: relative;
          background-size: 100% 100%;
          width: 200rpx;
          height: 200rpx;
          display: flex;
          align-items: center;
          justify-content: center;
          margin: 0 20rpx;
          /* margin-right: 10rpx; */
        }

        .level-icon {
          width: 71rpx;
          height: 28rpx;
          line-height: 28rpx;
          position: absolute;
          background-size: 100% 100%;
          left: 10rpx;
          top: 10rpx;
          font-size: 17rpx;
          box-sizing: border-box;
          padding-left: 30rpx;
        }

        .goods_image {
          width: 170rpx;
          height: 170rpx;
        }

        .goods_info {
          display: flex;
          flex-direction: column;
          justify-content: space-between;
          flex: 1;
          height: 170rpx;

          .goods_name {
            width: 350rpx;
            font-size: 28rpx;
            -webkit-line-clamp: 3;
            display: -webkit-box;
            -webkit-box-orient: vertical;
            overflow: hidden;
          }

          .goods_price_box {
            display: flex;
            justify-content: space-between;
            align-items: center;

            .goods_price {
              display: flex;
              align-items: baseline;
              font-size: 28rpx;
              color: #DED000;

              .goods_price_ext {
                background-color: rgba(255, 255, 255, 0.2);
                font-size: 22rpx;
                margin-left: 10rpx;
                padding: 0 10rpx;
                text-align: center;
                color: white;
                border-radius: 5rpx;
              }
            }

            .goods_num {
              font-size: 30rpx;
              color: white;
              margin-right: 40rpx;
            }
          }

        }
      }

    }
  }

  .freight {
    display: flex;
    justify-content: space-between;
    position: fixed;
    bottom: 120rpx;
    left: 0;
    width: 100%;
    height: 50rpx;
    align-items: center;
    background-color: #383400;
    font-size: 24rpx;

    .freight_title {
      margin-left: 30rpx;
    }

    .freight_rule {
      margin-right: 30rpx;
      color: #DED000;
    }
  }

  .bottom {
    display: flex;
    justify-content: space-between;
    background-color: #141414;
    position: fixed;
    bottom: 0rpx;
    left: 0;
    width: 100%;
    height: 120rpx;
    align-items: center;
    color: #000;
    color: white;

    .bottom_select {
      display: flex;
      align-items: center;

      .bottom_select_icon {
        width: 32rpx;
        height: 32rpx;
        margin: 0 20rpx;
      }
    }

    .bottom_text {
      display: flex;
      align-items: center;
      font-weight: normal;
      font-size: 24rpx;
      margin-left: 70rpx;

      .bottom_text_num {
        margin: 0 10rpx;
        color: #DED000;
        /* font-weight: bold; */
        font-size: 24rpx;
      }
    }

    .exchange_btn {
      height: 84rpx;
      line-height: 84rpx;
      text-align: center;
      width: 228rpx;
      margin: 0 20rpx;
      border-radius: 5rpx;
      font-size: 28rpx;
      background-size: 100% 100%;
      background-image: url('https://www.dingxians.cn/upload/user_static/common/btn-primary-small.png');
      filter: grayscale(1);

      &.exchange {
        filter: none;
        color: #fff;
      }
    }
  }

}</style>